<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="../assets/css/layui.css">
    <link rel="stylesheet" href="../assets/css/view.css"/>
    <link rel="stylesheet" href="../assets/css/style.css"/>
</head>
<body class="layui-tank">
<div class="layui-card">
    <form class="layui-form" id="froms" method="post" enctype="multipart/form-data">

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>宠物名称</label>
            <div class="layui-input-block">
                <input type="text" name="goodsName" id="goodsName" lay-verify="title" autocomplete="off" placeholder=""
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>宠物价格</label>
            <div class="layui-input-block">
                <input type="number" name="goodsPrice" id="goodsPrice" lay-verify="required|number" autocomplete="off"
                       placeholder=""
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>宠物类型</label>
            <div class="layui-input-block">
                <select name="type1Id" id="type1Id" lay-verify="required" lay-filter="type1Id">
                    <option value="0" disabled="disabled">请选择类型</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>宠物功能</label>
            <div class="layui-input-block">
                <select name="type2Id" id="type2Id" lay-verify="required" lay-filter="type2Id">
                    <option value="0">请选择功能</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>宠物品种</label>
            <div class="layui-input-block">
                <select name="type3Id" id="type3Id" lay-verify="required" lay-filter="type3Id">
                    <option value="0">请选择品种</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>宠物库存</label>
            <div class="layui-input-block">
                <input type="number" name="goodsNum" id="goodsNum" lay-verify="title" lay-verify="required|number"
                       autocomplete="off"
                       placeholder=""
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">宠物图片</label>
            <div class="layui-input-block">
                <input type="file" name="goodsImage" id="goodsImage"></input>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>宠物描述</label>
            <div class="layui-input-block">
                <textarea style="resize:none;height: 80px" lay-verify="required" type="textarea" name="goodsDesc"
                          id="goodsDesc"
                          lay-verify="title"
                          autocomplete="off"
                          placeholder="" class="layui-input"></textarea>
            </div>
        </div>
    </form>
</div>
<div class=" tkbtnfxd">
    <button type="button" class="layui-btn  layui-btn-normal float-right" id="sure">确定</button>
    <button type="button" class="layui-btn layui-btn-primary float-right" id="clean">取消</button>
</div>

<script src="../assets/layui.all.js"></script>
<script>
    layui.use(['table', 'layer', 'jquery'], function () {
        var table = layui.table,
            $ = layui.jquery,
            layer = layui.layer;


        getType1List();

        function getType1List() {
            $.ajax({
                type: "GET",
                url: "/getType1List",
                dataType: "json",
                success: function (data) {
                    $.each(data, function (index, item) {
                        $('#type1Id').append(new Option(item.type1Name, item.type1Id));
                        $('#type1Id').val("0");
                    });
                    layui.form.render("select");
                }
            });
        }

        layui.form.on('select(type1Id)', function (data) {
            /*当宠物类别发生改变时*/
            $.ajax({
                type: "GET",
                url: "/getType2ListByType1Id/" + data.value,
                dataType: "json",
                success: function (data) {
                    if (data.length === 0) {
                        layer.msg("没有对应宠物功能！", {
                            icon: 0, time: 800, shade: 0.2, end: function () {
                                $("#type2Id").find('option').remove();
                                $("#type2Id").append("<option value=\"\">" + 请选择功能 + "</option>");
                                $("#type2Id").val("");
                            }
                        });
                    } else {
                        $("#type2Id").find('option').remove();
                        for (var i = 0; i < data.length; i++) {
                            $("#type2Id").append("<option value=\"" + data[i].type2Id + "\">" + data[i].type2Name +
                                "</option>");
                            $("#type2Id").val(data[0].type2Id);
                        }
                    }
                    getType3ListByType2Id(data[0].type2Id);
                    layui.form.render("select");
                }
            });
        });

        layui.form.on('select(type2Id)', function (data) {
            /*当宠物功能发生改变时*/
            getType3ListByType2Id(data.value);
        });


        function getType3ListByType2Id(data) {
            $.ajax({
                type: "GET",
                url: "/getType3ListByType2Id/" + data,
                dataType: "json",
                success: function (data) {
                    if (data.length === 0) {
                        layer.msg("没有对应宠物品种！", {
                            icon: 0, time: 800, shade: 0.2, end: function () {
                                $("#type3Id").find('option').remove();
                                $("#type2Id").append("<option value=\"\">" + 请选择品种 + "</option>");
                            }
                        });
                    } else {
                        $("#type3Id").find('option').remove();
                        for (var i = 0; i < data.length; i++) {
                            $("#type3Id").append("<option value=\"" + data[i].type3Id + "\">" + data[i].type3Name +
                                "</option>");
                            $("#type3Id").val(data[0].type3Id);
                        }
                    }
                    layui.form.render("select");
                }
            });
        }


        $("#sure").click(function () {
            var formdata = new FormData();
            formdata.append("goodsImage", $("#goodsImage")[0].files[0]);
            formdata.append("goodsName", $("#goodsName").val());
            formdata.append("goodsPrice", $("#goodsPrice").val());
            formdata.append("goodsDesc", $("#goodsDesc").val());
            formdata.append("goodsNum", $("#goodsNum").val());
            formdata.append("type1Id", $("#type1Id").val());
            formdata.append("type2Id", $("#type2Id").val());
            formdata.append("type3Id", $("#type3Id").val());
            index = layer.load(1, {shade: [0.1, '#fff']});
            parent.layer.msg('正在添加...', {icon: 16, shade: 0.3, time: 5000});
            $.ajax({
                url: "/uploadFile",
                type: "POST",
                data: formdata,
                processData: false,
                contentType: false,
                success: function (r) {
                    layer.closeAll('loading');
                    if (r === "success") {
                        parent.layer.msg("添加成功!", {
                            icon: 1, time: 800, shade: 0.2, end: function () {
                                parent.layer.closeAll();
                                window.location.reload();
                            }
                        });
                    } else if (r === "error_size") {
                        parent.layer.msg("图片大小为5MB之内,请重试!", {
                            icon: 2, time: 800, shade: 0.2
                        });
                    } else {
                        parent.layer.msg("添加失败,请重试!", {
                            icon: 2, time: 800, shade: 0.2
                        });
                    }
                    layer.close(index);
                },
                error: function (e) {
                    layer.closeAll('loading');
                    parent.layer.msg("系统错误!", {icon: 2, time: 800, shade: 0.2});
                }
            })
        });

        $("#clean").click(function () {
            parent.layer.closeAll()
        });

    });
</script>
</body>
</html>
